define(["jquery", "carOperate"], function (a, CarOperate) {
    'use strict'
    var user = "";
    // var findCookie="";
    var carProduct = 0;
    var menuStatus = false;
    function getCookie(str) {
        var cookieStr = document.cookie;
        var cookieList = cookieStr.split(";");
        for (var i = 0; i < cookieList.length; i++) {
            var cooks = cookieList[i].split("=");
            console.log(str, cooks[0], cooks[1]);
            if (cooks[0].trim() == str) {
                user = cooks[1].trim();
                break;
            }

        }
    }
    function renderShopBag() {
        if(user=='') return false;
        var carOperate = new CarOperate();
        carProduct = carOperate.getStorage();
        console.log($(".buyNum"))
        $(".buyNum").html(carOperate.getTotalCount());
    }
    function getUser() {
        console.log(user);
        if (user !== "") {
            $(".user").css({ "display": "block" });
            $(".unlogin").css({ "display": "none" });
            $(".user").find("span").text(user);

        } else {
            $(".user").css({ "display": "none" });
            $(".unlogin").css({ "display": "block" });
        }
    }
    function bindEvent() {
        $(".user").hover(showUserMenu,hideUserMenu);
        // $(".user").on("focusout", );
        console.log($(".login-out"),$(".user-menu"))
        $(".login-out").on("click", loginOut);
    }
    function showUserMenu() {
        $(".user-menu").css({
            display: `${menuStatus ? "none" : "block"}`,
        })
        menuStatus = !menuStatus;
    }
    function hideUserMenu() {
        $(".user-menu").css({
            display: "none",
        })
        menuStatus = false;
    }
    function loginOut() {
        var exp = new Date();
        exp.setTime(exp.getDate() - 1);
        console.log(exp);
        document.cookie = 'jp_user=' + user + ';expires=' + exp;
        location.reload();
    }
    function init() {
        getCookie("jp_user");
        getUser();
        renderShopBag();
        bindEvent();
    }
    init();
    return user;
})